package com.hnust.service.impl;

import com.hnust.dao.CollegeDao;
import com.hnust.dao.TeacherDao;
import com.hnust.domain.Teacher;
import com.hnust.domain.record.AddOneTeacher;
import com.hnust.domain.record.SearchTeacherInfo;
import com.hnust.domain.record.SimpleTeacherInfo;
import com.hnust.domain.record.TeacherInfo;
import com.hnust.service.TeacherService;
import com.hnust.util.IDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建人：张汝往
 * 创建时间：2020/10/6
 * 描述：
 */
@Service("teacherService")
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private CollegeDao collegeDao;

    /**
     * 根据条件搜索老师信息
     * @param collegeId 学院ID
     * @param searchTeacherInfo 老师搜索条件类
     * @return
     */
    @Override
    public List<SimpleTeacherInfo> searchSimpleTeacherInfo(String collegeId, SearchTeacherInfo searchTeacherInfo) {
        List<Teacher> teachers=teacherDao.findTeacherBySearch(collegeId,searchTeacherInfo);
        //非空判断
        if(teachers!=null){
            List<SimpleTeacherInfo> teacherInfos=new ArrayList<>();
            for(Teacher teacher:teachers){
                String collegeName=collegeDao.findNameByCollegeId(teacher.getColId());
                //数据封装
                SimpleTeacherInfo s=new SimpleTeacherInfo(
                        teacher.getTeaId(),teacher.getTeaNumber(),
                        teacher.getName(),teacher.getPassword(),
                        collegeName
                );
                //数据添加
                teacherInfos.add(s);
            }
            return teacherInfos;
        }else {
            return null;
        }

    }

    /**
     * 获取老师详细信息
     * @param teacherId 老师ID
     * @return
     */
    @Override
    public TeacherInfo getTeacherInfo(String teacherId) {
        Teacher teacher=teacherDao.findTeacherById(teacherId);
        if(teacher!=null){
            String collegeName=collegeDao.findNameByCollegeId(teacher.getColId());
            //数据封装
            return new TeacherInfo(
                    new SimpleTeacherInfo(
                            teacher.getTeaId(),teacher.getTeaNumber(),
                            teacher.getName(),teacher.getPassword(),
                            collegeName
                    ),teacher.getPhoNumber(),teacher.getEMail()
            );
        }else {
            return null;
        }

    }

    /**
     * 删除老师
     * @param teacherId 老师ID
     * @return
     */
    @Override
    public Boolean deleteTeacher(String teacherId) {
        try{
            return teacherDao.deleteTeacherByID(teacherId)>=1;
        }catch (Exception e){
            return false;
        }

    }

    /**
     * 批量删除老师
     * @param teacherIds 老师ID们
     * @return
     */
    @Override
    @Transactional
    public Boolean massDeleteTeacher(List<String> teacherIds) {
        for(String teacherId:teacherIds){
           teacherDao.deleteTeacherByID(teacherId);
        }
        return true;
    }

    /**
     * 修改老师信息
     * @param teacherInfo 要修改的老师信息类
     * @return
     */
    @Override
    public Boolean modifyTeacher(TeacherInfo teacherInfo) {
        Teacher t=new Teacher();
        if(teacherInfo!=null){
            t.setTeaId(teacherInfo.getSimpleTeacherInfo().getTeacherId());
            t.setTeaNumber(teacherInfo.getSimpleTeacherInfo().getAccount());
            t.setName(teacherInfo.getSimpleTeacherInfo().getName());
            t.setPassword(teacherInfo.getSimpleTeacherInfo().getPassword());
            t.setEMail(teacherInfo.getEmail());
            t.setPhoNumber(teacherInfo.getPhone());
            return teacherDao.updateTeacher(t);
        }else{
            return false;
        }
    }


    @Override
    @Transactional
    public Boolean addTeachers(List<Teacher> teachers, String collegeId) throws Exception {
        int i=0;
        try{
            for(Teacher teacher : teachers){
                i++;
                teacherDao.insertTeacher(teacher.getTeaId(),teacher.getTeaNumber(),teacher.getName(),teacher.getPassword(),teacher.getPhoNumber(),teacher.getEMail(),collegeId);
            }
        }catch (Exception e) {
            if ("class org.springframework.dao.DuplicateKeyException".equals(e.getClass().toString()))
                throw new Exception("数据已存在-问题发生在第" + i + "组数据");
            else{
                throw e;
            }
        }
        return true;
    }

    // 添加一个老师信息
    @Override
    public Boolean addOneTeacher(AddOneTeacher teacher, String colId) {
        return teacherDao.insertTeacher(IDUtil.getPrimaryKey(), teacher.getAccount(), teacher.getName(), teacher.getPassword(), teacher.getPhone(), teacher.getEmail(), colId) >= 1;
    }
}
